package com.quik.web.user.action;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;

import com.quik.common.Constants;
import com.quik.dto.PaginationDto;
import com.quik.entity.SysAccount;
import com.quik.exception.ErrorCode;
import com.quik.web.user.service.AccountService;

/**
 * 功能说明：
 * 		帐户业务控制
 * 
 * @author jenny
 *
 */
@Controller
@RequestMapping("/account")
public class AccountController {
	private static Logger log = (Logger)Logger.getLogger(AccountController.class);
	
	@Autowired
	private AccountService accountService;
	
	/**
	 * 处理Spring mvc装载日期类型
	 * @param request
	 * @param binder
	 * @throws Exception
	 */
	@InitBinder
	protected void init(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		   dateFormat.setLenient(false);
		   binder.registerCustomEditor(Date.class, new CustomDateEditor(
		     dateFormat, false));
		//initDataBinder(request, binder);
	}
	
	/**
	 * 进入新增用户界面
	 * @return
	 */
	@RequestMapping(value = "initAddUser")
	public ModelAndView initAddUser(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/account/user/addUser");
		
		try{
			List list = accountService.findUserTypeCode();
			
			String result = (String)request.getAttribute("result");
			if(result != null && result.length() > 0)
				mav.addObject("result", result);
			
			
			mav.addObject("gender_man", SysAccount.GENDER_MAN);
			mav.addObject("gender_woman", SysAccount.GENDER_WOMAN);
			mav.addObject("statusActive", SysAccount.ACCOUNT_STATUS_ACTIVE);
			mav.addObject("statusInactive", SysAccount.ACCOUNT_STATUS_INACTIVE);
			mav.addObject("userTypeList", list);
		}catch(Exception e){
			e.printStackTrace();
			log.error("AccountController initAddUser error: " + e.getMessage());
		}
		
		return mav;
	}
	
	/**
	 * 进入用户管理界面
	 * @return
	 */
	@RequestMapping(value = "initUser", method = RequestMethod.GET)
	public ModelAndView initUser(){
		ModelAndView mav = new ModelAndView("/account/user/userManage");
		
		try{
			List list = accountService.findUserTypeCode();
			
			mav.addObject("userTypeList", list);
			mav.addObject("gender_man", SysAccount.GENDER_MAN);
			mav.addObject("statusActive", SysAccount.ACCOUNT_STATUS_ACTIVE);
		}catch(Exception e){
			e.printStackTrace();
			log.error("AccountController initUser error: " + e.getMessage());
		}
		
		return mav;
	}
	
	/**
	 * 根据用户条件查询
	 * @param page
	 * @param rows
	 * @param userType
	 * @param staffNo
	 * @param loginName
	 * @return
	 */
	@RequestMapping(value = "queryUser")
	@ResponseBody
	public PaginationDto queryUser(Integer page, Integer rows, Integer userType, String staffNo, String loginName){
		PaginationDto dto = new PaginationDto(0, null);
		try{
   		    int intPage = (page == null || page == 0) ? 1:page;  //当前页
		    int pageSize = (rows == null || rows == 0) ? Constants.PAGE_SIZE : rows;  //每页显示的记录
			
		    int totalCount = accountService.findUserCount(userType, staffNo, loginName);  //总记录数
			
			List<SysAccount> list = accountService.findUser(userType, staffNo, loginName, intPage, pageSize);
			
		    dto.setRows(list);
		    dto.setTotal(totalCount);
			
		}catch(Exception e){
			e.printStackTrace();
			log.error("AccountController queryUser error: " + e.getMessage());
		}
		
		return dto;
	}
	
	/**
	 * 新增用户
	 * @param sysAccount
	 * @return
	 */
	@RequestMapping(value = "addUser", method = RequestMethod.POST)
	public ModelAndView addUser(SysAccount sysAccount, HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/account/user/addUser");
		SysAccount account = (SysAccount)request.getSession().getAttribute(Constants.SESSION_ACCOUNT);
		
		try{
			if(sysAccount.getLogin_name() == null && sysAccount.getLogin_name().trim().length() <= 0){
				mav.addObject("result", ErrorCode.RESULT_ADD_USER_INVALIDDATA);
			}else{
				sysAccount.setCrt_user(account.getStaff_no());
				sysAccount.setCrt_date(new java.util.Date(System.currentTimeMillis()));
				accountService.addUser(sysAccount);
				mav.addObject("result", ErrorCode.RESULT_ADD_USER_SUCCESS);
				mav.setViewName("forward:/account/initAddUser");
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error("AccountController addUser error: " + e.getMessage());
			mav.addObject("result", ErrorCode.SYSTEM_ERROR);
		}
		
		return mav;
	}
	
	/**
	 * ajax 验证工号是否重复  
	 * @param staffNo
	 * @return
	 */
	@RequestMapping(value = "checkStaffNo", method = RequestMethod.POST)
	public ResponseEntity<String> checkStaffNo(String staffNo){
		boolean result = false;
		try{
			result = accountService.checkStaffNo(staffNo);
		}catch(Exception e){
			e.printStackTrace();
			log.error("AccountController checkStaffNo error: " + e.getMessage());
		}
		
		return new ResponseEntity<String>(String.valueOf(result), HttpStatus.OK);
	}
	
	/**
	 * ajax 验证登录名是否重复
	 * @param loginName
	 * @return
	 */
	@RequestMapping(value = "checkLoginName", method = RequestMethod.POST)
	public ResponseEntity<String> checkLoginName(String loginName){
		boolean result = false;
		try{
			result = accountService.checkLoginName(loginName);
		}catch(Exception e){
			e.printStackTrace();
			log.error("AccountController checkLoginName error: " + e.getMessage());
		}
		
		return new ResponseEntity<String>(String.valueOf(result), HttpStatus.OK);
	}
	
	
	
}
